<template>
  <div class="mainBox flex flex_column">
    <!-- 搜索     -->
    <div class="btnCon flex flex_between">
      <div class="plop flex">
        <el-select
                        filterable v-model="search.warehouseCode" placeholder="请选择仓库" size="small" @change="inventoryChange">
          <el-option
            v-for="item in warhouseList"
            :key="item.warehouseID"
            :label="item.warehouseName"
            :value="item.warehouseCode"
          />
        </el-select>
        <el-button type="primary" class="ml15" size="small" icon="el-icon-plus" v-permission="'MenuCode5-8-1'" @click="addInventory">新建
        </el-button>
      </div>
      <!--      搜索框-->
      <div class="search">
        <el-input
          v-model="search.likeWords"
          placeholder="输入关键字"
          size="small"
          @keyup.enter.native="getAllLists"
        >
          <el-button
            slot="append"
            v-loading="searchLoading"
            type="primary"
            icon="el-icon-search"
            :disabled="searchLoading"
            element-loading-spinner="el-icon-loading"
            @click="getAllLists"
          >
            搜索
          </el-button>
        </el-input>
      </div>
    </div>
    <!--    高级搜索版块-->
    <div class="seniorSearch flex flex_end">
      <div class="flex">
        <!--          <div class="c_3F61F1">高级搜索</div>-->
        <!--          <span class="line"/>-->
        <div class="pointer" @click="Refresh"><i class="el-icon-refresh-right" /></div>
        <span class="line" />
        <!--        表头设置-->
        <div class="setting flex">
          <HeadSeting :list-data.sync="tableHead" :table-code="$config.takeStockList" @reset="getTableHead" />
        </div>
      </div>
    </div>
    <!--table板块-->
    <div ref="tableCon" class="tableCon">
      <el-table
        v-show="tableHead.length>0"
        id="emtable"
        ref="multipleTable"
        v-loading="searchLoading"
        :data="dataLists"
        :height="tableHeight || 'auto'"
        border
        highlight-current-row
        fit
        style="width: 100%"
        @header-dragend="surverWidth"
      >
        <!-- 复选框       -->
        <!--        <el-table-column type="selection" width="40" fixed="left" />-->
        <template v-for="(fruit,index) in tableHead">
          <el-table-column
            v-if="fruit.visible"
            :key="index"
            :label="fruit.columnName"
            :min-width="fruit.columnWidth || 110"
            :prop="fruit.columnCode"
            show-overflow-tooltip
            :fixed="fruit.isFloat?'left':false"
          >
            <template slot-scope="scope">
              {{ scope.row[fruit.columnCode] }}
            </template>
          </el-table-column>
        </template>
        <el-table-column label="操作" width="70px" fixed="right" align="center">
          <template slot-scope="scope">
            <div>
              <el-popover
                :ref="`popover-${scope.$index}`"
                placement="bottom-end"
                popper-class="tableOp"
                width="160"
                :popper-options="{ boundariesElement: 'opBox', removeOnDestroy: true }"
                trigger="click"
              >
                <div class="operation">
                  <ul v-for="(arry,arryIndex) in operations" :key="arryIndex">
                    <template v-for="(op,opIndex) in arry">
                      <template v-if="op.key==='see'">
                        <li
                          v-if="scope.row.takeStockState===1 || scope.row.takeStockState===2"
                          :key="opIndex"
                          @click="optionsClick(op.key,scope.row)"
                        >{{ op.text }}
                        </li>
                      </template>
                      <li v-else-if="scope.row.takeStockState===1" :key="opIndex" @click="optionsClick(op.key,scope.row)">{{ op.text }}</li>
                    </template>
                  </ul>
                </div>
                <div slot="reference" class="opBox pointer"><span class="el-icon-more c_3F61F1" /></div>
              </el-popover>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <!-- 骨架屏     -->
      <div v-if="tableHead.length==0" class="p16">
        <el-skeleton :rows="6" animated />
      </div>
    </div>
   <!-- 分页 S-->
    <div class="d_pag" style="display:felx;justify-content: flex-start;width:100%">
      <pagination
        :total="total||0"
        :page.sync="search.pageIndex"
        :limit.sync="search.pageSize"
        @pagination="getAllLists"
      />
    </div>
    <!-- 分页 E-->
    <!--  盘点新建  -->
    <el-dialog
      :title="addTitle"
      :visible.sync="isAddInventory"
      :close-on-click-modal="false"
      custom-class="bodyNoPadDialog bigDialog"
      width="85%"
      @open="handleOpenDialog"
      @close="closeDialog"
    >
      <div class="dialogDody">
        <div class="subTip">
          <span class="mr15"><span class="mr5">盘点单号：</span>{{ inventoryForm.takeStockBillNo }}</span>
          <span class="mr15"><span
            class="mr5"
          >创建日期：</span>{{ inventoryForm.createTime || dayjs().format('YYYY-MM-DD') }}</span>
          <span><span class="mr5">创建人员：</span>{{ inventoryForm.createPersonName || name }}</span>
        </div>
        <div class="addWuliaoBox">
          <el-form
            ref="inventoryForm"
            :model="inventoryForm"
            label-width="80px"
            label-position="left"
            class="dialogForm dialogInlineForm"
          >
            <!-- <el-row>
              <el-col :span="8">
                <el-form-item label="入库类型">
                  <el-select
                        filterable
                    v-model="inventoryForm.warehouseCode"
                    placeholder="请选择仓库"
                    size="small"
                    :disabled="inventoryForm.takeStockBillNo?true:false"
                    @change="inventoryChange"
                  >
                    <el-option
                      v-for="item in warhouseList"
                      :key="item.warehouseID"
                      :label="item.warehouseName"
                      :value="item.warehouseCode"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row> -->
            <el-form-item label="盘点备注" class="inlineText">
              <el-input
                v-model="inventoryForm.takeStockRemark"
                type="textarea"
                :rows="1"
                placeholder="盘点备注"
                size="mini"
                clearable
              />
            </el-form-item>
          </el-form>
          <div class="tableBox flex flex_column flex1">
            <div class="tt flex flex_between">
              <span class="fw">盘点明细</span>
              <div class="composeRadio mr15 flex">
                <div class="">
                  <el-radio-group v-model="searchInfo.takeStockState" size="mini" @change="stateQuery">
                    <el-radio-button label="0">待盘点</el-radio-button>
                    <el-radio-button label="1">已盘点</el-radio-button>
                  </el-radio-group>
                </div>
                <!-- <div class="comafter flex">
                  <el-radio-group v-model="searchInfo.takeStockResult" size="mini" @change="resultQuery">
                    <el-radio-button label="2">盘赢</el-radio-button>
                    <el-radio-button label="3">盘亏</el-radio-button>
                    <el-radio-button label="1">持平</el-radio-button>
                    <el-radio-button label=" ">全部</el-radio-button>
                  </el-radio-group>
                </div> -->
              </div>
            </div>
            <div class="GtableCon flex1" ref="GtableCon">
              <el-table
                v-loading="detailLoading"
                :data="inventoryDetailList"
                :row-class-name="tableRowClassName"
                :summary-method="getSummaries"
                show-summary
                border
                style="width: 100%;"
              >
                <el-table-column
                  type="index"
                  label="序号"
                  width="50"
                />
                <el-table-column
                  label="物料编号"
                  prop="stockGoodsCode"
                  show-overflow-tooltip
                  min-width="110"
                />
                <el-table-column
                  label="物料名称"
                  prop="goodsName"
                  show-overflow-tooltip
                  min-width="110"
                />
                <el-table-column
                  label="规格型号"
                  prop="stockGoodsName"
                  min-width="190"
                />
                <el-table-column
                  label="物料分组"
                  prop="groupName"
                  min-width="90"
                />
                <el-table-column
                  label="物料类型"
                  prop="categoryName"
                  min-width="90"
                />
                <el-table-column
                  label="供应商"
                  prop="supplierName"
                  show-overflow-tooltip
                  min-width="90"
                />
                <el-table-column
                  label="物料单位"
                  prop="goodsUnitName"
                  min-width="80"
                />
                <el-table-column
                  label="每米/m²"
                  prop="specificationJsValue"
                  min-width="80"
                />
                <el-table-column
                  label="当前库存数量"
                  prop="originalStockQuantity"
                  min-width="110"
                />
                <el-table-column
                  label="盘点单价"
                  show-overflow-tooltip
                  prop="takeStockPrice"
                  min-width="90"
                />
                <el-table-column
                  label="库存总金额"
                  show-overflow-tooltip
                  prop="originalStockSumValue"
                  min-width="100"
                />
                <el-table-column
                  label="实盘数量"
                  min-width="110"
                >
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.takeStockQuantity" @input="handleNumChange(scope.row,scope.$index)" placeholder="请输入" size="mini" />
                  </template>
                </el-table-column>
                <el-table-column
                  label="盈亏结果"
                  show-overflow-tooltip
                  prop="takeStockResult"
                  min-width="90"
                >
                  <template slot-scope="{row}">
                    <template v-if="row.takeStockResult === 1">
                      持平
                    </template>
                    <template v-else-if="row.takeStockResult === 2">
                      <div class="c_54a55b">盘赢</div>
                    </template>
                    <template v-else-if="row.takeStockResult === 3">
                      <div class="c_e15c45">盘亏</div>
                    </template>
                    <template v-else>
                      <div>未盘</div>
                    </template>
                  </template>
                </el-table-column>
                <el-table-column
                  label="盈亏数量"
                  show-overflow-tooltip
                  prop="gainorLossQuantity"
                  min-width="90"
                >
                  <template slot-scope="{row}">
                    <template v-if="row.takeStockResult === 1">
                      0
                    </template>
                    <template v-else-if="row.takeStockResult === 2">
                      <div class="c_54a55b">{{ row.gainorLossQuantity }}</div>
                    </template>
                    <template v-else-if="row.takeStockResult === 3">
                      <div class="c_e15c45">{{ row.gainorLossQuantity }}</div>
                    </template>
                    <template v-else>
                      <div>{{ row.gainorLossQuantity }}</div>
                    </template>
                  </template>
                </el-table-column>
                <el-table-column
                  label="盈亏总金额"
                  prop="gainorLossSumValue"
                  min-width="100"
                >
                  <template slot-scope="{row}">
                    <template v-if="row.takeStockResult === 1">
                      0
                    </template>
                    <template v-else-if="row.takeStockResult === 2">
                      <div class="c_54a55b">+ {{ row.gainorLossSumValue }}</div>
                    </template>
                    <template v-else-if="row.takeStockResult === 3">
                      <div class="c_e15c45">{{ row.gainorLossSumValue }}</div>
                    </template>
                    <template v-else>
                      <div>{{ row.gainorLossSumValue }}</div>
                    </template>
                  </template>
                </el-table-column>
                <el-table-column
                  label="备注"
                  min-width="150"
                >
                  <template slot-scope="{row}">
                    <el-input v-model="row.takeStockRemark" placeholder="请输入" size="mini" />
                  </template>
                </el-table-column>
              </el-table>
            </div>
          </div>
        </div>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click="isAddInventory=false">取 消</el-button>
        <!-- 确认当前盘点     -->
        <el-button
          v-loading="addLoading"
          :disabled="addLoading"
          element-loading-spinner="el-icon-loading"
          type="warning"
          size="mini"
          @click="confirmAdd"
        >保存并继续
        </el-button>
        <!--  保存      -->
        <el-button
          v-loading="addLoading"
          :disabled="addLoading"
          element-loading-spinner="el-icon-loading"
          type="primary"
          size="mini"
          @click="savaAdd"
        >保存并关闭
        </el-button>
      </div>
    </el-dialog>
    <!--  查看盘点  -->
    <el-dialog
      title="查看盘点"
      :visible.sync="isSeeinventory"
      :close-on-click-modal="false"
      custom-class="bodyNoPadDialog bigDialog"
      width="85%"
      @close="closeDialog"
    >
      <div class="dialogDody">
        <div class="subTip">
          <span class="mr15"><span class="mr5">盘点单号：</span>{{ inventoryForm.takeStockBillNo }}</span>
          <span class="mr15"><span
            class="mr5"
          >创建日期：</span>{{ inventoryForm.createTime || dayjs().format('YYYY-MM-DD') }}</span>
          <span><span class="mr5">创建人员：</span>{{ inventoryForm.createPersonName }}</span>
        </div>
        <div class="addWuliaoBox">
          <el-form
            ref="inventoryForm"
            :model="inventoryForm"
            disabled
            label-width="80px"
            label-position="left"
            class="dialogForm dialogInlineForm"
          >
            <!-- <el-row>
              <el-col :span="8">
                <el-form-item label="入库类型">
                  <el-select
                        filterable
                    v-model="inventoryForm.warehouseCode"
                    placeholder="请选择仓库"
                    size="small"
                    :disabled="inventoryForm.takeStockBillNo?true:false"
                    @change="inventoryChange"
                  >
                    <el-option
                      v-for="item in warhouseList"
                      :key="item.warehouseID"
                      :label="item.warehouseName"
                      :value="item.warehouseCode"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row> -->
            <el-form-item label="盘点备注" class="inlineText">
              <el-input
                v-model="inventoryForm.takeStockRemark"
                type="textarea"
                :rows="1"
                placeholder="盘点备注"
                size="mini"
                clearable
              />
            </el-form-item>
          </el-form>
          <div class="tableBox flex flex_column flex1">
            <div class="tt flex flex_between">
              <span class="fw">盘点明细</span>
              <div class="composeRadio mr15 flex">
                <div class="combefore flex">
                  <el-radio-group v-model="searchInfo.isTakeStock" size="mini" @change="stateQuerySee">
                    <el-radio-button label="0">待盘点({{ inventoryForm.statData?inventoryForm.statData.waitTakeStockQuantity:0 }})</el-radio-button>
                    <el-radio-button label="1">已盘点({{ inventoryForm.statData?inventoryForm.statData.finishedTakeStockQuantity:0 }})</el-radio-button>
                  </el-radio-group>
                </div>
                <div class="comafter flex">
                  <el-radio-group v-model="searchInfo.takeStockResult" size="mini" @change="resultQuery">
                    <el-radio-button label="2">盘赢({{ inventoryForm.statData?inventoryForm.statData.panYingQuantity:0 }})</el-radio-button>
                    <el-radio-button label="3">盘亏({{ inventoryForm.statData?inventoryForm.statData.panKuiQuantity:0 }})</el-radio-button>
                    <el-radio-button label="1">持平({{ inventoryForm.statData?inventoryForm.statData.chiPingQuantity:0 }})</el-radio-button>
                    <el-radio-button label="-1">全部({{ inventoryForm.statData?inventoryForm.statData.allTakeStockQuantity:0 }})</el-radio-button>
                  </el-radio-group>
                </div>
              </div>
            </div>
            <div class="GtableCon flex1" ref="GtableConSee">
              <el-table
                v-loading="detailLoading"
                :data="inventoryDetailList"
                :row-class-name="tableRowClassName"
                :summary-method="getSummaries"
                show-summary
                border
                style="width: 100%"
              >
                <el-table-column
                  type="index"
                  label="序号"
                  width="50"
                />
                <el-table-column
                  label="物料编号"
                  prop="stockGoodsCode"
                  show-overflow-tooltip
                  min-width="110"
                />
                <el-table-column
                  label="物料名称"
                  prop="goodsName"
                  show-overflow-tooltip
                  min-width="110"
                />
                <el-table-column
                  label="规格型号"
                  prop="stockGoodsName"
                  min-width="190"
                />
                <el-table-column
                  label="物料分组"
                  prop="groupName"
                  min-width="90"
                />
                <el-table-column
                  label="物料类型"
                  prop="categoryName"
                  min-width="90"
                />
                <el-table-column
                  label="供应商"
                  prop="supplierName"
                  show-overflow-tooltip
                  min-width="90"
                />
                <el-table-column
                  label="物料单位"
                  prop="goodsUnitName"
                  min-width="80"
                />
                <el-table-column
                  label="每米/m²"
                  prop="specificationJsValue"
                  min-width="80"
                />
                <el-table-column
                  label="当前库存数量"
                  prop="originalStockQuantity"
                  min-width="110"
                />
                <el-table-column
                  label="盘点单价"
                  show-overflow-tooltip
                  prop="takeStockPrice"
                  min-width="90"
                />
                <el-table-column
                  label="库存总金额"
                  show-overflow-tooltip
                  prop="originalStockSumValue"
                  min-width="100"
                />
                <el-table-column
                  label="实盘数量"
                  min-width="110"
                  prop="takeStockQuantity"
                />
                <el-table-column
                  label="盈亏结果"
                  show-overflow-tooltip
                  prop="takeStockResult"
                  min-width="90"
                >
                  <template slot-scope="{row}">
                    <template v-if="row.takeStockResult === 1">
                      {{ row.takeStockResultName }}
                    </template>
                    <template v-else-if="row.takeStockResult === 2">
                      <div class="c_54a55b">{{ row.takeStockResultName }}</div>
                    </template>
                    <template v-else-if="row.takeStockResult === 3">
                      <div class="c_e15c45">{{ row.takeStockResultName }}</div>
                    </template>
                    <template v-else>
                      <div>未盘</div>
                    </template>
                  </template>
                </el-table-column>
                <el-table-column
                  label="盈亏数量"
                  show-overflow-tooltip
                  prop="gainorLossQuantity"
                  min-width="90"
                >
                  <template slot-scope="{row}">
                    <template v-if="row.takeStockResult === 1">
                      0
                    </template>
                    <template v-else-if="row.takeStockResult === 2">
                      <div class="c_54a55b">{{ row.gainorLossQuantity }}</div>
                    </template>
                    <template v-else-if="row.takeStockResult === 3">
                      <div class="c_e15c45">{{ row.gainorLossQuantity }}</div>
                    </template>
                    <template v-else>
                      <div>{{row.gainorLossQuantity}}</div>
                    </template>
                  </template>
                </el-table-column>
                <el-table-column
                  label="盈亏总金额"
                  prop="gainorLossSumValue"
                  min-width="100"
                >
                  <template slot-scope="{row}">
                    <template v-if="row.takeStockResult === 1">
                      0
                    </template>
                    <template v-else-if="row.takeStockResult === 2">
                      <div class="c_54a55b">+ {{ row.gainorLossSumValue }}</div>
                    </template>
                    <template v-else-if="row.takeStockResult === 3">
                      <div class="c_e15c45">{{ row.gainorLossSumValue }}</div>
                    </template>
                    <template v-else>
                      <div>{{ row.gainorLossSumValue }}</div>
                    </template>
                  </template>
                </el-table-column>
                <el-table-column
                  label="备注"
                  min-width="150"
                  prop="takeStockRemark"
                />
              </el-table>
            </div>
          </div>
        </div>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click="isSeeinventory=false">取 消</el-button>
        <!--  修改      -->
        <!-- <el-button
          v-if="ops==='see'"
          v-loading="addLoading"
          :disabled="addLoading"
          element-loading-spinner="el-icon-loading"
          type="primary"
          size="mini"
          @click="updateClick"
        >修改
        </el-button> -->
        <!--  确认      -->
        <el-button
          v-if="ops==='confirm'"
          v-loading="addLoading"
          :disabled="addLoading"
          element-loading-spinner="el-icon-loading"
          type="primary"
          size="mini"
          @click="confirmClick"
        >确认</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { arraySort } from '@/utils'
import Pagination from '@/components/Pagination'
import HeadSeting from '@/components/HeadSeting'
import { mapGetters } from 'vuex'
import { string } from 'yargs'

export default {
  name: 'Inventory',
  components: { Pagination, HeadSeting },
  computed: {
    ...mapGetters([
      'name'
    ])
  },
  data() {
    return {
      ops: 'see', // 操作
      addTitle: '新建盘点',
      addLoading: false,
      detailLoading: false, // 盘点明细加载效果
      isAddInventory: false, // 新建盘点是否打开
      isSeeinventory: false, // 查看盘点弹框是否打开
      takeStockID: null, // 盘点主键ID
      inventoryForm: {
        warehouseCode: '',
        takeStockID: '',
        takeStockRemark: '',
        takeStockBillNo: '',
        detailList: []
      }, // 新建盘点表单
      inventoryDetailList: [], // 盘点明细列表
      operations: [
        [{ text: '查看', key: 'see' }, { text: '修改', key: 'modify' }],
        [{ text: '确认盘点', key: 'confirm' }],
        [{ text: '删除', key: 'delete' }]
      ], // 表格操作
      searchLoading: false, // 表格加载效果
      search: {
        pageIndex: 1,
        pageSize: 30,
        warehouseCode: '',
        takeStockState: '',
        takeStockResult: ''
      }, // 盘点分页列表搜索条件
      searchInfo: {
        takeStockBillNo:'', //盘点单号
        isTakeStock: '', //是否盘点：0待盘点；1已盘点，-1查询全部
        takeStockResult: '-1', //盘点结果：0未盘点；1盘平（无盈亏）；2盘盈；3盘亏，-1查询全部
        takeStockState:0
      }, // 盘点明细列表搜索条件
      dataLists: [], // 盘点列表数据
      detailList:[],
      arr1:[],
      arr2:[],
      total: 0,
      tableHead: [], // 表头
      tableHeight: null,
      jcINfoTableHeight: null,
      warhouseList: [], // 仓库下拉列表
      warhouseListDetail:[] //新建仓库下拉列表
    }
  },
  created() {
    // 采购列表表格头部
    this.getTableHead()
    // 获取仓库下拉列表
    this.getWarehouseList()
  },
  mounted() {
    // 表格最大高度
    this.calHeight()
  },
  updated() {
    this.$nextTick(() => {
      this.$refs.multipleTable.doLayout()
    })
  },
  methods: {

    //实盘数量输入事件
    handleNumChange(row,index){
     this.inventoryDetailList[index].gainorLossQuantity = row.takeStockQuantity - row.originalStockQuantity
      this.inventoryDetailList[index].gainorLossSumValue = (row.gainorLossQuantity*row.takeStockPrice).toFixed(4)
      if(this.inventoryDetailList[index].gainorLossSumValue<0){
        this.inventoryDetailList[index].takeStockResult = 3
      }else if(this.inventoryDetailList[index].gainorLossSumValue == 0){
        this.inventoryDetailList[index].takeStockResult = 1
      }else{
        this.inventoryDetailList[index].takeStockResult = 2
      }
      this.$forceUpdate()
    },

    // 删除未确认的盘点单
    deleteTakeStick(id) {
      const parmas = {
        takeStockID: id
      }
      this.$confirm('确认删除当前盘点, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$api.warehouse.DeleteTakeStock(parmas).then(() => {
          this.getAllLists()
          this.$message({
            message: '已删除',
            type: 'success'
          })
        })
      })
    },
    // 确认盘点
    confirmClick() {
      const parmas = {
        takeStockID: this.takeStockID
      }
      this.$confirm('确认当前盘点, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$api.warehouse.SureFinishTakeStock(parmas).then(() => {
          this.$message({
            message: '已确认',
            type: 'success'
          })
          this.getAllLists()
          this.isSeeinventory = false
        })
      })
    },
    updateClick() {
      this.isAddInventory = true
      this.isSeeinventory = false
      this.addTitle = '修改盘点'
    },
    // 确认当前盘点
    confirmAdd() {
      if(this.addTitle == '新建盘点'){
        this.addLoading = true
        this.inventoryForm.detailList = [...this.arr1,...this.arr2]
        const parmas = this.inventoryForm
        this.$api.warehouse.AddTakeStock(parmas).then(res => {
          this.inventoryForm.takeStockBillNo = res
          this.$message.success('确认成功')
          this.getInventoryInfoList(res)
          this.addTitle = '修改盘点'
          this.getAllLists()
          this.addLoading = false
        }).catch(() => {
          this.addLoading = false
        })
      }else if(this.addTitle == '修改盘点'){
        this.addLoading = true
        this.inventoryForm.detailList = [...this.arr1,...this.arr2]
        const parmas = this.inventoryForm
        this.$api.warehouse.UpdateTakeStockBill(parmas).then(res => {
          this.inventoryForm.takeStockBillNo = res
          this.$message.success('修改成功')
          this.getInventoryInfoList(res)
          this.getAllLists()
          this.addLoading = false
        }).catch(() => {
          this.addLoading = false
        })
      }
    },
    // 保存当前盘点
    savaAdd() {
      this.confirmAdd()
      this.isAddInventory = false
    },
    // 合计方法
    getSummaries(param) {
      const { columns, data } = param
      const sums = []
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '合计'
          return
        }
        if(column.label !== '盈亏结果' && column.label !== '规格型号' &&column.label!= '每米/m²'){
          const values = data.map(item => Number(item[column.property]))
          if (!values.every(value => isNaN(value))) {
            sums[index] = values.reduce((prev, curr) => {
              const value = Number(curr)
              if (!isNaN(value)) {
                return prev + curr
              } else {
                return prev
              }
            }, 0)
            if(sums[index]){
              // if(String(sums[index]).indexOf('.') > -1){
              //   console.log(sums[index])
              //   sums[index] = String(sums[index]).split('.')[1]
              //   debugger
              //   if(sums[index].length>4){
              //     sums[index] = Number(sums[index]).toFixed(4)
              //   }
              // }else{
              //   sums[index] = sums[index]
              // }
              sums[index] = parseFloat(sums[index].toFixed(4))
            }
          } else {
            sums[index] = ' '
          }
        }
      })

      return sums
    },
    // 给选中一行添加class
    tableRowClassName({ row }) {
      if (row.takeStockResult === 1) {
        return ''
      } else if (row.takeStockResult === 2) {
        return 'green'
      } else if (row.takeStockResult === 3) {
        return 'red'
      } else {
        return 'blue'
      }
    },
    // 仓库改变事件
    inventoryChange() {
      this.getAllLists()
      this.inventoryForm.warehouseCode = this.search.warehouseCode
      this.getInventoryList()
    },
    // 盘点明细 筛选
    stateQuery(val) {
      this.searchInfo.takeStockResult = ''
        if (val === '0') {
          this.inventoryDetailList = this.arr1
        } else {
          this.inventoryDetailList = this.arr2
        }
    },

    stateQuerySee(val){
      this.searchInfo.takeStockResult = ''
      this.searchInfo.isTakeStock = val
      this.$api.warehouse.QueryTakeStockBillsMxList(this.searchInfo).then((res)=>{
        this.inventoryDetailList = res
      })
    },

    resultQuery(val) {
      if(val == -1){
        this.searchInfo.isTakeStock = -1
      }else{
        this.searchInfo.isTakeStock = 1
      }
      this.searchInfo.takeStockBillNo = this.inventoryForm.takeStockBillNo
      this.searchInfo.takeStockResult = Number(this.searchInfo.takeStockResult)
      this.$api.warehouse.QueryTakeStockBillsMxList(this.searchInfo).then((res)=>{
        this.inventoryDetailList = res
      })
      // if (this.inventoryForm.takeStockBillNo) {
      //   this.getInventoryInfoList(this.inventoryForm.takeStockBillNo)
      // } else {
      //   this.inventoryDetailList = []
      // }
    },
    // 新建盘点
    addInventory() {
      this.$api.warehouse.JuadgeIsHaveTakeStockBill({warehouseCode:this.search.warehouseCode}).then((res)=>{
          this.isAddInventory = true
          this.addTitle = '新建盘点'
          this.inventoryForm.takeStockRemark = ''
          this.getInventoryList()
          this.$nextTick(() => {
            this.jcINfoTableHeight = this.$refs.GtableCon.offsetHeight
          })
      })
    },
    // 没有盘点单号，获取盘点明细列表
    getInventoryList() {
      this.detailLoading = true
      const parmas = {
        warehouseCode: this.inventoryForm.warehouseCode
      }
      this.$api.warehouse.GetWarehousesBatchStockGoods(parmas).then(res => {
        this.inventoryDetailList = res
        this.detailList = res

        this.arr1 = this.detailList.filter((item)=>{
            return item.isTakeStock == 0
          })
          this.arr2 = this.detailList.filter((item)=>{
            return item.isTakeStock == 1
          })
        this.detailLoading = false
      }).catch(() => {
        this.detailLoading = false
      })
    },
    // 有盘点单号，获取盘点明细列表
    getInventoryInfoList(id) {
      this.detailLoading = true
      const parmas = {
        takeStockID:0,
        takeStockBillNo:id
      }
      this.$api.warehouse.GetSingleTakeStock(parmas).then(res => {
        this.inventoryForm = res
        this.inventoryDetailList = res.detailList
        this.detailList = res.detailList
        this.arr1 = this.detailList.filter((item)=>{
            return item.isTakeStock == 0
          })
          this.arr2 = this.detailList.filter((item)=>{
            return item.isTakeStock == 1
          })
        this.detailLoading = false
      }).catch(() => {
        this.detailLoading = false
      })
    },
    // 表格操作方法
    optionsClick(key, data) {
      if (key === 'modify') {
        // 修改
        this.isAddInventory = true
        this.addTitle = '修改盘点'
        this.inventoryForm.takeStockBillNo = data.takeStockBillNo
        this.getInventoryInfoList(data.takeStockBillNo)
        this.$nextTick(() => {
          this.jcINfoTableHeight = this.$refs.GtableCon.offsetHeight
        })
      } else if (key === 'see') {
        // 查看
        this.ops = 'see'
        this.isSeeinventory = true
        this.inventoryForm.takeStockBillNo = data.takeStockBillNo
        this.searchInfo.takeStockBillNo = data.takeStockBillNo
        this.searchInfo.takeStockResult = '-1'
        this.getInventoryInfoList(data.takeStockBillNo)
        //this.getInventoryInfoList(data.takeStockBillNo)
      //   this.$api.warehouse.QueryTakeStockBillsMxList(this.searchInfo).then((res)=>{
      //   this.inventoryDetailList = res
      // })
        this.$nextTick(() => {
          this.jcINfoTableHeight = this.$refs.GtableConSee.offsetHeight
        })
      } else if (key === 'confirm') {
        // 确认盘点
        this.ops = 'confirm'
        this.isSeeinventory = true
        this.takeStockID = data.takeStockID
        this.inventoryForm.takeStockBillNo = data.takeStockBillNo
        this.getInventoryInfoList(data.takeStockBillNo)
        this.$nextTick(() => {
          this.jcINfoTableHeight = this.$refs.GtableConSee.offsetHeight
        })
      } else if (key === 'delete') {
        // 删除
        this.deleteTakeStick(data.takeStockID)
      }
    },
    // 保存表头列宽度
    surverWidth(newWidth, oldWidth, column) {
      setTimeout(() => {
        const heads = [...this.tableHead]
        heads.forEach(item => {
          if (item.columnCode === column.property) {
            item.columnWidth = column.width
            return
          }
        })
        this.saveHead(heads)
      }, 100)
    },
    // 刷新
    Refresh() {
      this.search = this.$options.data().search
      // 盘点分页列表
      this.getAllLists()
    },
    // 获取仓库下拉列表
    getWarehouseList() {
      this.$api.warehouse.getWarehouseList().then(res => {
        this.warhouseList = res
        this.inventoryForm.warehouseCode = res[0].warehouseCode
        this.search.warehouseCode = res[0].warehouseCode
        // 盘点分页列表
        this.getAllLists()
      })
    },
    // 盘点表头
    getTableHead() {
      const parmas = {
        tableCode: this.$config.takeStockList
      }
      this.$api.common.getAllTableHead(parmas).then(res => {
        res.sort(arraySort('columnSort'))
        this.tableHead = res
      })
    },
    // 盘点分页列表
    getAllLists() {
      const parmas = this.search
      this.searchLoading = true
      this.$api.warehouse.GetMatchPageTakeStocks(parmas).then(res => {
        this.dataLists = res.list
        this.dataLists.forEach((item)=>{
            for(let i in item){
              if(!item[i] && item[i] !== 0){
                item[i] = '--'
              }
            }
          })
        this.total = res.totalCount
        this.searchLoading = false
      }).catch(() => {
        this.searchLoading = false
      })
    },
    calHeight() {
      this.$nextTick(() => {
        const rect = this.$refs.tableCon.getBoundingClientRect()
        this.tableHeight = rect.height
      })
    },
    // 关闭弹框事件
    closeDialog() {

    },

    handleOpenDialog(){
      this.searchInfo.takeStockState = 0
    }
  }
}
</script>
<style lang="scss" scoped>
.addWuliaoBox {
  padding: 10px 16px;

  .tableBox {
    margin-top: 16px;
    border: 1px solid #e6e6e6;
    border-bottom: none;
    border-radius: 8px 8px 0 0;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    .GtableCon{
      width: 100%;
    }
    .tt {
      width: 100%;
      height: 38px;
      line-height: 38px;
      padding-left: 16px;
      background: #f7f8fa;
      border-radius: 8px 8px 0 0;
    }
  }
}

.subTip {
  border-bottom: 1px #e6e6e6 solid;
  background: #f5f6f9;
  padding: 8px 16px;

}
</style>
